package hk.m4s.lr.repair.test.ui.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.timer.TimeButton;
import framework.common.zanetwork.core.ResponseCallback;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.VcodeModel;
import hk.m4s.lr.repair.test.service.AccountSerive;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/23.
 *  验证手机号
 */

public class InvatePhoneActivity extends UeBaseActivity implements View.OnClickListener{
    private EditText newPhoneEt,codeEt;
    private TextView phone_name;
    private Button edit_button;
    private Context context;
    private TimeButton send_code_btn;
    private String phoneNumVal="",codeVal="";
    String code = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_invate_phone);
        context=this;
        hiddenFooter();
        hiddenNewMessage();
        showGoBackBtn();
        setTitleText("验证手机号");
        newPhoneEt=(EditText)findViewById(R.id.edit_phone);
        codeEt=(EditText)findViewById(R.id.edit_code);
        edit_button=(Button) findViewById(R.id.edit_button);
        edit_button.setOnClickListener(this);
        send_code_btn=(TimeButton) findViewById(R.id.send_code_btn);
        send_code_btn.onCreate(savedInstanceState);
        send_code_btn.setTextAfter("秒后重新获取").setTextBefore("获取手机验证码").setLenght(60 * 1000);
        send_code_btn.setPhoneNumber(phoneNumVal);
        send_code_btn.setOnClickListener(this);
        newPhoneEt.addTextChangedListener(new TextWatcher(
        ) {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                phoneNumVal=s.toString();
                if(phoneNumVal.equals("")||phoneNumVal.length()<11){
                    send_code_btn.setEnabled(false);
                }else{
                    send_code_btn.setPhoneNumber(phoneNumVal);
                    send_code_btn.setEnabled(true);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.edit_button:
                if(newPhoneEt.getText().toString().trim().equals("")){
                    ToastUtil.toast(context,"请填写手机号");
                    return;
                }else if (!codeEt.getText().toString().equals(code)) {
                    ToastUtil.toast(context, "请确定验证码正确");
                    return;
                }
                phoneNumVal=newPhoneEt.getText().toString().trim();
                Intent intens = new Intent(InvatePhoneActivity.this, SetPayPassActivity.class);
                startActivity(intens);
                finish();
                break;
            case R.id.send_code_btn:
                 if(newPhoneEt.getText().toString().trim().equals("")){
                     ToastUtil.toast(context,"请填写手机号");
                     send_code_btn.setEnabled(false);
                     return;
                 }
                sendSms();
                break;
        }
    }
    public void sendSms() {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("phone", phoneNumVal.toString().trim());
        parmas.put("type", "4");
        AccountSerive.sendSms(this, parmas, new ResponseCallback<VcodeModel>() {
            @Override
            public void onSuccess(VcodeModel map) {
                code = map.getVerifyCode();
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
                ToastUtil.toast(context, errorMsg);
            }
        });
    }
}
